package com.winglish.as3.exam.toeicspeaking 
{
	import com.winglish.as3.loader.SoundPlay;
	import com.winglish.as3.record.Recorder;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import com.winglish.as3.exam.toeicspeaking.vo.TSVolumeMicTestVo;
	
	/**
	 * ...
	 * @author charmjin
	 */
	public class TSVolumeMicTest extends TSVolumeMicTestVo
	{
		private const mcAlpha:Number = .5;
		private var sound:SoundPlay;
		private var record:Recorder;
		private var offBgm:Boolean = false;
		
		public function TSVolumeMicTest() 
		{
			sound = new SoundPlay();
			sound.addEventListener("loadComplete", soundLoadComplete);
			sound.addEventListener(Event.SOUND_COMPLETE, soundComplete);
			record = new Recorder();
		}
		
		public function soundSet(filename:String):void {
			if (filename != null) {
				sound.soundLoader(filename);
			}
		}
		
		/**
		 * sound load complete event
		 * @param	e
		 */
		private function soundLoadComplete(e:Event):void {
			e.target.soundStop();
			dispatchEvent(new Event("soundLoadComplete"));
		}
		
		/**
		 * sound play complete event
		 * @param	e
		 */
		private function soundComplete(e:Event):void {
			//trace("sound complete");
		}
		
		/**
		 * 버튼 셋팅
		 * @param	path
		 */
		public function setting(path:MovieClip):void {
			if (path != null) {
				mapping(path);
				
				playMc.addEventListener(MouseEvent.CLICK, playMcClickEvent);
				playMc.buttonMode = true;
				stopMc.alpha = recMc.alpha = recStopMc.alpha = recListenMc.alpha = recListenStopMc.alpha = mcAlpha;
				finishBtn.alpha = mcAlpha;
				finishBtn.enabled = false;
				
				//임시
				//finishBtn.addEventListener(MouseEvent.CLICK, finishBtnClickEvent);
			}
		}
		
		/**
		 * Listening Test - Play Btn Click Event
		 * @param	e
		 */
		private function playMcClickEvent(e:MouseEvent):void {
			allGotoAndStop();
			targetSet(e.target as MovieClip);
			
			stopMc.alpha = 1;
			stopMc.buttonMode = true;
			if (!stopMc.hasEventListener(MouseEvent.CLICK)) stopMc.addEventListener(MouseEvent.CLICK, stopMcClickEvent);
			
			if (!offBgm) {	//처음 클릭 이벤트 진행되면 bgm등 기타 사운드 음소거를 위한 이벤트 발생.
				offBgm = true;
				dispatchEvent(new Event("bgmSoundStop"));
			}
		
			sound.soundPlay();
			
		}
		
		/**
		 * Listening Test - Stop Btn Click Event
		 * @param	e
		 */
		private function stopMcClickEvent(e:MouseEvent):void {
			allGotoAndStop();
			targetSet(e.target as MovieClip);
			
			playMc.alpha =  recMc.alpha = 1;
			recMc.buttonMode = true;
			playMc.gotoAndStop(1);
			if (!playMc.hasEventListener(MouseEvent.CLICK)) playMc.addEventListener(MouseEvent.CLICK, playMcClickEvent);
			if (!recMc.hasEventListener(MouseEvent.CLICK)) recMc.addEventListener(MouseEvent.CLICK, recMcClickEvent);
			
			sound.soundStop();
		}
		
		/**
		 * Recording Test - Rec Btn Click Event
		 * @param	e
		 */
		private function recMcClickEvent(e:MouseEvent):void {
			allGotoAndStop();
			targetSet(e.target as MovieClip);
			
			recStopMc.alpha = 1;
			recStopMc.buttonMode = true;
			if (!recStopMc.hasEventListener(MouseEvent.CLICK)) recStopMc.addEventListener(MouseEvent.CLICK, recStopMcClickEvent);
			
			record.start();
		}
		
		/**
		 * Recording Test - RecStop Btn Click Event
		 * @param	e
		 */
		private function recStopMcClickEvent(e:MouseEvent):void {
			allGotoAndStop();
			targetSet(e.target as MovieClip);
			
			recMc.alpha = recListenMc.alpha = 1;
			recMc.buttonMode = recListenMc.buttonMode = true;
			if (!recMc.hasEventListener(MouseEvent.CLICK)) recMc.addEventListener(MouseEvent.CLICK, recMcClickEvent);
			if (!recListenMc.hasEventListener(MouseEvent.CLICK)) recListenMc.addEventListener(MouseEvent.CLICK, recListenMcClickEvent);
			
			record.stop();
		}
		
		/**
		 * Recording Test - RecListen Btn Click Event
		 * @param	e
		 */
		private function recListenMcClickEvent(e:MouseEvent):void {
			allGotoAndStop();
			targetSet(e.target as MovieClip);
			
			recListenStopMc.alpha = 1;
			recListenStopMc.buttonMode = true;
			if (!recListenStopMc.hasEventListener(MouseEvent.CLICK)) recListenStopMc.addEventListener(MouseEvent.CLICK, recListenStopMcClickEvent);
			
			record.listen();
		}
		
		/**
		 * Recording Test - RecListenStop Btn Click Event
		 * @param	e
		 */
		private function recListenStopMcClickEvent(e:MouseEvent):void {
			allGotoAndStop();
			targetSet(e.target as MovieClip);
			
			playMc.alpha = recMc.alpha = recListenMc.alpha = 1;
			playMc.buttonMode = recMc.buttonMode = recListenMc.buttonMode = true;
			if (!playMc.hasEventListener(MouseEvent.CLICK)) playMc.addEventListener(MouseEvent.CLICK, playMcClickEvent);
			if (!recMc.hasEventListener(MouseEvent.CLICK)) recMc.addEventListener(MouseEvent.CLICK, recMcClickEvent);
			if (!recListenMc.hasEventListener(MouseEvent.CLICK)) recListenMc.addEventListener(MouseEvent.CLICK, recListenMcClickEvent);

			record.stopListen();
			
			if (finishBtn.alpha == mcAlpha) {
				finishBtn.alpha = 1;
				finishBtn.enabled = true;
				finishBtn.addEventListener(MouseEvent.CLICK, finishBtnClickEvent);
			}
		}
		
		/**
		 * 모든 버튼 무비클립 비활성화
		 */
		private function allGotoAndStop():void {
			playMc.gotoAndStop(1);
			stopMc.gotoAndStop(1);
			recMc.gotoAndStop(1);
			recStopMc.gotoAndStop(1);
			recListenMc.gotoAndStop(1);
			recListenStopMc.gotoAndStop(1);
			
			playMc.alpha = stopMc.alpha = recMc.alpha = recStopMc.alpha = recListenMc.alpha = recListenStopMc.alpha = mcAlpha;
			playMc.buttonMode = stopMc.buttonMode = recMc.buttonMode = recStopMc.buttonMode = recListenMc.buttonMode = recListenStopMc.buttonMode = false;
			
			if (playMc.hasEventListener(MouseEvent.CLICK)) playMc.removeEventListener(MouseEvent.CLICK, playMcClickEvent);
			if (stopMc.hasEventListener(MouseEvent.CLICK)) stopMc.removeEventListener(MouseEvent.CLICK, stopMcClickEvent);
			if (recMc.hasEventListener(MouseEvent.CLICK)) recMc.removeEventListener(MouseEvent.CLICK, recMcClickEvent);
			if (recStopMc.hasEventListener(MouseEvent.CLICK)) recStopMc.removeEventListener(MouseEvent.CLICK, recStopMcClickEvent);
			if (recListenMc.hasEventListener(MouseEvent.CLICK)) recListenMc.removeEventListener(MouseEvent.CLICK, recListenMcClickEvent);
			if (recListenStopMc.hasEventListener(MouseEvent.CLICK)) recListenStopMc.removeEventListener(MouseEvent.CLICK, recListenStopMcClickEvent);
		}
		
		/**
		 * target 무비클립 활성화
		 * @param	target
		 */
		private function targetSet(target:MovieClip):void {
			target.alpha = 1;
			target.buttonMode = true;
			target.gotoAndStop(2);
		}
		
		/**
		 * finishBtn Click Event
		 * @param	e
		 */
		private function finishBtnClickEvent(e:MouseEvent):void {
			sound.soundStop();
			record.stop();
			record.stopListen();
			dispatchEvent(new Event("finishBtnClick"));
		}
		

	}

}